﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace Terraria.ModLoader.IO
{
	// Token: 0x020002E7 RID: 743
	public class TagCompound : IEnumerable<KeyValuePair<string, object>>, IEnumerable, ICloneable
	{
		// Token: 0x06001C88 RID: 7304 RVA: 0x0043688C File Offset: 0x00434A8C
		public T Get<T>(string key)
		{
			object obj = null;
			this.dict.TryGetValue(key, out obj);
			T result;
			try
			{
				result = TagIO.Deserialize<T>(obj);
			}
			catch (Exception innerException)
			{
				throw new IOException(string.Format("NBT Deserialization (type={0},", typeof(T)) + string.Format("entry={0})", TagPrinter.Print(new KeyValuePair<string, object>(key, obj))), innerException);
			}
			return result;
		}

		// Token: 0x06001C89 RID: 7305 RVA: 0x004368FC File Offset: 0x00434AFC
		public void Set(string key, object value)
		{
			if (value == null)
			{
				this.Remove(key);
				return;
			}
			try
			{
				this.dict.Add(key, TagIO.Serialize(value));
			}
			catch (IOException innerException)
			{
				string text = "value=" + value;
				if (value.GetType().ToString() != value.ToString())
				{
					text = string.Concat(new object[]
					{
						"type=",
						value.GetType(),
						",",
						text
					});
				}
				throw new IOException(string.Format("NBT Serialization (key={0},{1})", key, text), innerException);
			}
		}

		// Token: 0x06001C8A RID: 7306 RVA: 0x00014514 File Offset: 0x00012714
		public bool ContainsKey(string key)
		{
			return this.dict.ContainsKey(key);
		}

		// Token: 0x06001C8B RID: 7307 RVA: 0x00014522 File Offset: 0x00012722
		public bool Remove(string key)
		{
			return this.dict.Remove(key);
		}

		// Token: 0x06001C8C RID: 7308 RVA: 0x00014530 File Offset: 0x00012730
		[Obsolete]
		public T GetTag<T>(string key)
		{
			return this.Get<T>(key);
		}

		// Token: 0x06001C8D RID: 7309 RVA: 0x00014539 File Offset: 0x00012739
		[Obsolete]
		public void SetTag(string key, object value)
		{
			this.Set(key, value);
		}

		// Token: 0x06001C8E RID: 7310 RVA: 0x00014543 File Offset: 0x00012743
		[Obsolete]
		public bool HasTag(string key)
		{
			return this.ContainsKey(key);
		}

		// Token: 0x06001C8F RID: 7311 RVA: 0x0001454C File Offset: 0x0001274C
		[Obsolete]
		public bool RemoveTag(string key)
		{
			return this.Remove(key);
		}

		// Token: 0x06001C90 RID: 7312 RVA: 0x00014555 File Offset: 0x00012755
		public byte GetByte(string key)
		{
			return this.Get<byte>(key);
		}

		// Token: 0x06001C91 RID: 7313 RVA: 0x0001455E File Offset: 0x0001275E
		public short GetShort(string key)
		{
			return this.Get<short>(key);
		}

		// Token: 0x06001C92 RID: 7314 RVA: 0x00014567 File Offset: 0x00012767
		public int GetInt(string key)
		{
			return this.Get<int>(key);
		}

		// Token: 0x06001C93 RID: 7315 RVA: 0x00014570 File Offset: 0x00012770
		public long GetLong(string key)
		{
			return this.Get<long>(key);
		}

		// Token: 0x06001C94 RID: 7316 RVA: 0x00014579 File Offset: 0x00012779
		public float GetFloat(string key)
		{
			return this.Get<float>(key);
		}

		// Token: 0x06001C95 RID: 7317 RVA: 0x00014582 File Offset: 0x00012782
		public double GetDouble(string key)
		{
			return this.Get<double>(key);
		}

		// Token: 0x06001C96 RID: 7318 RVA: 0x0001458B File Offset: 0x0001278B
		public byte[] GetByteArray(string key)
		{
			return this.Get<byte[]>(key);
		}

		// Token: 0x06001C97 RID: 7319 RVA: 0x00014594 File Offset: 0x00012794
		public int[] GetIntArray(string key)
		{
			return this.Get<int[]>(key);
		}

		// Token: 0x06001C98 RID: 7320 RVA: 0x0001459D File Offset: 0x0001279D
		public string GetString(string key)
		{
			return this.Get<string>(key);
		}

		// Token: 0x06001C99 RID: 7321 RVA: 0x000145A6 File Offset: 0x000127A6
		public IList<T> GetList<T>(string key)
		{
			return this.Get<List<T>>(key);
		}

		// Token: 0x06001C9A RID: 7322 RVA: 0x000145AF File Offset: 0x000127AF
		public TagCompound GetCompound(string key)
		{
			return this.Get<TagCompound>(key);
		}

		// Token: 0x06001C9B RID: 7323 RVA: 0x000145B8 File Offset: 0x000127B8
		public bool GetBool(string key)
		{
			return this.Get<bool>(key);
		}

		// Token: 0x06001C9C RID: 7324 RVA: 0x0043699C File Offset: 0x00434B9C
		public short GetAsShort(string key)
		{
			object obj = this.Get<object>(key);
			short? num = obj as short?;
			if (num != null)
			{
				return num.GetValueOrDefault();
			}
			byte? b = obj as byte?;
			if (b == null)
			{
				return 0;
			}
			return (short)b.GetValueOrDefault();
		}

		// Token: 0x06001C9D RID: 7325 RVA: 0x004369EC File Offset: 0x00434BEC
		public int GetAsInt(string key)
		{
			object obj = this.Get<object>(key);
			int? num = obj as int?;
			if (num != null)
			{
				return num.GetValueOrDefault();
			}
			short? num2 = obj as short?;
			if (num2 != null)
			{
				return (int)num2.GetValueOrDefault();
			}
			byte? b = obj as byte?;
			if (b == null)
			{
				return 0;
			}
			return (int)b.GetValueOrDefault();
		}

		// Token: 0x06001C9E RID: 7326 RVA: 0x00436A5C File Offset: 0x00434C5C
		public long GetAsLong(string key)
		{
			object obj = this.Get<object>(key);
			long? num = obj as long?;
			if (num == null)
			{
				return (long)((obj as int?) ?? ((int)((obj as short?) ?? ((short)((obj as byte?) ?? 0)))));
			}
			return num.GetValueOrDefault();
		}

		// Token: 0x06001C9F RID: 7327 RVA: 0x00436AEC File Offset: 0x00434CEC
		public double GetAsDouble(string key)
		{
			object obj = this.Get<object>(key);
			double? num = obj as double?;
			if (num == null)
			{
				return (double)((obj as float?) ?? 0f);
			}
			return num.GetValueOrDefault();
		}

		// Token: 0x06001CA0 RID: 7328 RVA: 0x00436B44 File Offset: 0x00434D44
		public object Clone()
		{
			TagCompound tagCompound = new TagCompound();
			foreach (KeyValuePair<string, object> keyValuePair in this)
			{
				tagCompound.Set(keyValuePair.Key, TagIO.Clone<object>(keyValuePair.Value));
			}
			return tagCompound;
		}

		// Token: 0x06001CA1 RID: 7329 RVA: 0x000145C1 File Offset: 0x000127C1
		public override string ToString()
		{
			return TagPrinter.Print(this);
		}

		// Token: 0x170001D9 RID: 473
		public object this[string key]
		{
			get
			{
				return this.Get<object>(key);
			}
			set
			{
				this.Set(key, value);
			}
		}

		// Token: 0x06001CA4 RID: 7332 RVA: 0x00014539 File Offset: 0x00012739
		public void Add(string key, object value)
		{
			this.Set(key, value);
		}

		// Token: 0x06001CA5 RID: 7333 RVA: 0x000145D2 File Offset: 0x000127D2
		public void Add(KeyValuePair<string, object> entry)
		{
			this.Set(entry.Key, entry.Value);
		}

		// Token: 0x06001CA6 RID: 7334 RVA: 0x000145E8 File Offset: 0x000127E8
		public void Clear()
		{
			this.dict.Clear();
		}

		// Token: 0x170001DA RID: 474
		// (get) Token: 0x06001CA7 RID: 7335 RVA: 0x000145F5 File Offset: 0x000127F5
		public int Count
		{
			get
			{
				return this.dict.Count;
			}
		}

		// Token: 0x06001CA8 RID: 7336 RVA: 0x00014602 File Offset: 0x00012802
		public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
		{
			return this.dict.GetEnumerator();
		}

		// Token: 0x06001CA9 RID: 7337 RVA: 0x00014614 File Offset: 0x00012814
		IEnumerator IEnumerable.GetEnumerator()
		{
			return this.GetEnumerator();
		}

		// Token: 0x040018CF RID: 6351
		private Dictionary<string, object> dict = new Dictionary<string, object>();
	}
}
